include("../../static/js/config.js");
include("../../static/js/common.js");


$(function () {
    var table;
    var token = localStorage.getItem("token");

    var options = {
        url: config.server + '/admin/blog/list',
        method: 'POST',
        headers: {Authorization: 'Bearer ' + token},
        elem: '#table',
        page: true,
        curr: 1,
        count: 1,
        limit: 10,
        limits: [10, 20, 30, 40, 50, 60, 70, 80, 90],
        cols:[[
            {type:'checkbox'},
            {field:'title', title:'标题'},
            {field:'description', title:'描述'},
            {field:'musicId', title:'音乐id'},
            {field:'createDate', title:'创建日期'},
            {fixed: 'right', title:'操作', toolbar: '#toolbar'}
        ]],
        where: {
            title:$("#title").val()
        },
        request: {
            pageName: 'current',
            limitName: 'size'
        },
        parseData: function (res) {
            return reqAfter(res, function (res) {
                var obj = {
                    "code": res.code === 200 ? 0 : res.code,
                    "msg": res.msg,
                    "count": res.data.total,
                    "data": res.data.records
                }
                return obj;
            });
        }
    };

    layui.use(['table'], function () {
        table = layui.table;
        table.render(options);

        table.on('tool(optEvent)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'edit':
                    window.open(config.url + '/page/blog/edit.html?id=' + data.id, "_blank");
                    break;
            }
        });

    });

    $("#searchBtn").click(function () {
        var title = $("#title").val();
        table.reload('table', {where: {title: title}});
    });

    $("#addBtn").click(function () {
        window.open(config.url + '/page/blog/add.html', "_blank");
    });

});

